package io.github.lopssh.mybatis.auditlog;

import io.github.lopssh.mybatis.auditlog.domain.Blog;
import io.github.lopssh.mybatis.auditlog.domain.BlogExample;
import io.github.lopssh.mybatis.auditlog.mapper.BlogMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

public class AuditLogTest
{

    public static void main(String[] args) throws IOException
    {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession session = sqlSessionFactory.openSession();
        try
        {
            BlogMapper mapper = session.getMapper(BlogMapper.class);
            // 测试主键更新
            Blog blog = new Blog();
            blog.setId(1);
            blog.setAuthor("lopssh");
            mapper.updateByPrimaryKeySelective(blog);
            // 测试删除
            mapper.deleteByPrimaryKey(1);
            // 测试插入（自增）
            blog = new Blog();
            blog.setAuthor("lopssh");
            blog.setStars(1111);
            mapper.insertSelective(blog);
            // 指定ID插入
            blog = new Blog();
            blog.setId(1);
            blog.setAuthor("lopssh");
            mapper.insert(blog);
            // 测试批量更新
            blog = new Blog();
            blog.setAuthor("1111111111111111");
            BlogExample example = new BlogExample();
            example.createCriteria().andAuthorEqualTo("lopssh");
            mapper.updateByExampleSelective(blog, example);
            // 其它任意类型的增删改…
            session.commit();
        } finally
        {
            session.close();
        }
    }
}
